<script>
import {userPage} from "@/api/user";
import {studentPage} from "@/api/student";

export default {
  name: "index",
  data() {
    return {
      currentPage:1,
      size:5,
      total:0,
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  },
  // 定义事件回调函数
  methods: {
    // 编辑数据回调函数
    handleEdit(index, row) {
      console.log(index, JSON.stringify(row));
    },
    // 删除数据回调函数
    handleDelete(index, row) {
      console.log(index, JSON.stringify(row));
    },
    // 复选框回调函数
    handleSelectionChange(val) {
      console.log(JSON.stringify(val))
    },
    // 监听每页显示数据条数的改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size =val
      this.initStudentPage()
    },
    // 监听页码的改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage =val
      this.initStudentPage()
    },
    initStudentPage() {
      // 实现分页
     studentPage(this.currentPage, this.size).then(response => {
        if (response.code == 200) {
          this.tableData = response.data
          this.total = response.count
        }
      })
    },
  },
  created() {
    this.initStudentPage()
  }
}
</script>

<template>
  <div id="container">
    <div id="btn-header">
      <el-button type="primary" plain size="small">新增</el-button>
      <el-button type="success" plain size="small">批量删除</el-button>
    </div>
    <!-- selection-change  检测复选框的选中状态       -->
    <el-table
        @selection-change="handleSelectionChange"
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="stuNo"
          label="学号"
      >
      </el-table-column>
      <el-table-column
          prop="stuName"
          label="姓名"
      >
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别"
      >
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄"
      >
      </el-table-column>
      <el-table-column
          prop="tel"
          label="联系方式"
      >
      </el-table-column>
      <el-table-column
          prop="clazz"
          label="班级">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div id="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
#btn-header {
  text-align: left;
  margin-top: 15px;
  margin-bottom: 15px;
}
#container{
  padding: 20px;
}
#pagination{
  margin-top: 15px;
}
</style>